import React from 'react'

import TestLifeCycleChild from './TestLifeCycleChild'

class TestLifeCycle extends React.Component {
  constructor (props) {
    super(props)
    console.log("Parent constructor")
    this.state = {
      comment: 'This is the first commment  '
    }
  }

  static getDerivedStateFromProps (nextProps, prevState) {
    console.log("Parent getDerivedStateFromProps")
    return {}
  }

  shouldComponentUpdate () {
    console.log("Parent shouldComponentUpdate")
    return true
  }

  getSnapshotBeforeUpdate (prevProps, prevState) {
    console.log("Parent getSnapshotBeforeUpdate")
    return null
  }

  componentDidUpdate () {
    console.log("Parent componentDidUpdate")
  }
  componentDidMount () {
    console.log("Parent componentDidMount")
    this.timer = setTimeout(() => {
      this.setState((prevState, props) => {
        return { comment: 'This is the second comment' }
      })
    }, 3000)
  }
  componentWillUnmount () {
    clearTimeout(this.timer)
  }
  render () {
    console.log("Parent render")
    return (
      <section>
        <TestLifeCycleChild comment={ this.state.comment }></TestLifeCycleChild>
      </section>
    )
  }
}

export default TestLifeCycle